﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace HaloReachApi.WP7.TestApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        private PlayerDetailsService detailsService;
        private ReachFileSearchService filesService;

        private string apikey = "[YOURAPIKEY]";

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            detailsService = new PlayerDetailsService(apikey);
            detailsService.GotError += DetailsService_GotError;
            detailsService.GotResult += DetailsService_GotResult;

            filesService = new ReachFileSearchService(apikey);
            filesService.GotError += (s,e) =>
                MessageBox.Show("File search error: " + e.Argument.Message);
            filesService.GotResult += FilesService_GotResult;
        }

        void FilesService_GotResult(object sender, Service4u2.Common.EventArgs<Responses.FileResponse> e)
        {
            var response = e.Argument;

            if (response.FileSets != null)
            {
                foreach (var set in response.FileSets)
                {
                    fileList.Items.Add(set);
                }
            }
            if (response.Files != null)
            {
                foreach (var file in response.Files)
                {
                    fileList.Items.Add(file);
                }
            }
        }

        ImageSourceConverter imgSourceCvt = new ImageSourceConverter();
        void DetailsService_GotResult(object sender, Service4u2.Common.EventArgs<Responses.PlayerDetailsResponse> e)
        {
            loadingText.Visibility = System.Windows.Visibility.Collapsed;

            var response = e.Argument;
            if (response.Player == null)
            {
                MessageBox.Show("Player not found.", "Not Found", MessageBoxButton.OK);

                DetailsRoot.Visibility = System.Windows.Visibility.Visible;
                searchBtn.IsEnabled = true;
                return;
            }

            titleTxt.Text = String.Format("Player Details: {0} ({1})", response.Player.gamertag, response.Player.service_tag);

            dailyTxt.Text = String.Format("Daily Challenges Completed: {0}", response.Player.daily_challenges_completed);
            weeklyTxt.Text = String.Format("Weekly Challenges Completed: {0}", response.Player.weekly_challenges_completed);

            lastTypeTxt.Text = String.Format("Last Game Type Played: {0}", response.Player.LastGameVariantClassPlayed);

            modelImg.Source = (ImageSource)imgSourceCvt.ConvertFrom("http://bungie.net" + response.PlayerModelUrlHiRes);

            DetailsRoot.Visibility = System.Windows.Visibility.Visible;

            searchBtn.IsEnabled = true;
        }

        void DetailsService_GotError(object sender, Service4u2.Common.EventArgs<Exception> e)
        {
            MessageBox.Show("Error: " + e.Argument.Message, "Uh-oh", MessageBoxButton.OK);
            
            loadingText.Visibility = System.Windows.Visibility.Collapsed;
            DetailsRoot.Visibility = System.Windows.Visibility.Visible;
            searchBtn.IsEnabled = true;
        }

        private void Search_Click(object sender, RoutedEventArgs e)
        {
            searchBtn.IsEnabled = false;
            loadingText.Visibility = System.Windows.Visibility.Visible;

            detailsService.GetPlayerDetailsNoStatsAsync(gamerTagBox.Text);
            filesService.GetFileSearchAsync(FileCategory.GameClip, "null", EngineFilter.Multiplayer);

            DetailsRoot.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
}